<?php


namespace app\api\controller\v1;

use app\api\validate\Cart as CartValidate;
use app\api\service\Cart as CartService;
use app\api\model\Cart as CartModel;
use app\api\validate\Paginate;
use app\common\exception\CartException;
use app\common\exception\MissException;

class Cart extends Base
{
    /**
     * 1.购物车List列表
     * 2.购物车添加商品
     * 3.更新购物车某个产品数量
     * 4.移除购物车某个产品
     * 5.全选中
     * 6.全不选中
     * 7.获取购物中所有商品数量总和
     */
    protected $beforeActionList = [
        'checkExclusiveScope' => ['only'=>'index,add,update,del,selectAll,unselectAll,countPrice']
    ];

    /**
     * method: get
     * url: api/v1/cart/index
     */
    public function index($page=1, $size=15)
    {
        (new Paginate())->goCheck();
        $user = $this->getCurrentUser();
        $pager = CartModel::getAll($user->id, $page, $size);
        if ($pager->isEmpty()) {
            throw new MissException([
                'msg' => "没找{$user->id}号的订单数据",
            ]);
        }
        return $this->ok($pager);
    }

    /**
     * method: post
     * url: api/v1/cart/add
     * id
     * num
     */
    public function add()
    {
        (new CartValidate())->scene('add')->goCheck();
        $user = $this->getCurrentUser();
        $flag = input('post.flag/d');
        $product_id = input('post.product_id');
        $sku_id = input('post.sku_id');
        $num = input('post.num');
        if(!CartService::create($user->id, $flag, $product_id, $sku_id, $num)){
            throw new CartException(['msg'=>'添加购物车失败', 'code'=>50001]);
        }

        $pager = CartModel::getRawAll($user->id);
        if ($pager->isEmpty()) {
            throw new MissException([
                'msg' => "没找{$user->id}号的订单数据",
            ]);
        }
        return $this->ok($pager);
    }

    /**
     * method: post
     * url: api/v1/cart/update
     * params: {"id":1,"num":3}
     */
    public function update()
    {

    }

    /**
     * method: post
     * url: api/v1/cart/del
     * params: {"id":1}
     */
    public function del()
    {
        (new CartValidate())->scene('del')->goCheck();
        $user = $this->getCurrentUser();
        $flag = input('post.flag/d');
        $product_id = input('post.product_id');
        $sku_id = input('post.sku_id');
        if(!CartService::delete($user->id, $flag, $product_id, $sku_id)){
            throw new CartException(['msg'=>"删除{$product_id}失败", 'code'=>50001]);
        }
        $pager = CartModel::getRawAll($user->id);
        if ($pager->isEmpty()) {
            throw new MissException([
                'msg' => "没找{$user->id}号的订单数据",
            ]);
        }
        return $this->ok($pager);
    }

    /**
     * method: post
     * url: api/v1/cart/check
     * params: {"id":1,"num":3}
     */
    public function selectAll()
    {

    }

    /**
     * method: post
     * url: api/v1/cart/check
     * params: {"id":1,"num":3}
     */
    public function unselectAll()
    {

    }

    /**
     * method: post
     * url: api/v1/cart/check
     * params: {"id":1,"num":3}
     */
    public function countPrice()
    {

    }
}